<?php
// Module Body

function sa_panel(&$data) {
	global $cfg, $T;
	$personaldata = $data['_personaldata'];

	/*
 *      Patrick Bores - WebUserPrefs - SpamAssassin user_prefs file editor
 *      Copyright (C) 2003  Patrick Bores
 *      Benjamin Story - Assumed Maintenance and SA 3.0 Fixes
 *		Peter Drake - modified for web-cp
 *      This program is free software; you can redistribute it and/or
 *      modify it under the terms of the GNU General Public License
 *      as published by the Free Software Foundation; either version 2
 *      of the License, or (at your option) any later version.
 *
 *      This program is distributed in the hope that it will be useful,
 *      but WITHOUT ANY WARRANTY; without even the implied warranty of
 *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *      GNU General Public License for more details.
 *
 *      You should have received a copy of the GNU General Public License
 *      along with this program; if not, write to the Free Software
 *      Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */
	// Preferences Source
	// Specify "db" if you store user preferences in a MySQL database.
	// Specify "file" if you store user preferences in user_prefs files.
	$prefs_source	= $cfg['SPAM_prefs_location'];
	// user_prefs File Location
	// If you typed "file" for $prefs_source above, set this variable
	// to the location of the user_prefs file. If you included
	// authorization above, use the $auth_user variable to include
	// the user_prefs file for the user that logged in.
	// $user_prefs	= "/path/to/user_prefs";
	$user_prefs	= $personaldata['root']."/.spamassassin/user_prefs";

	// MySQL Database Settings
	// If you specified "db" for $prefs_source above, configure the
	// database connection here. If you included authorization above,
	// use the $auth_user variable in $db_sauser;
	$db_table	= $cfg['SPAM_db_table'];

	// Obtain real username
	$uniquedata['username'] = $personaldata['username'];
	$uniquedata['id'] = $personaldata['id'];
	call_hook('user:get:username', $uniquedata);
	$unique = $uniquedata['username'];

	$db_sauser	= $unique;	// This is 'username';
	// The $panels_dir variable indicates which directory
	// contains your preferences panels. You can learn about
	// writing your own custom preferences panels in the file README.
	$panels_dir 	= $cfg['SPAM_panels_dir'];
	// Set this variable to "yes" if you want to group and sort
	// the rules list. Otherwise, the rules are just displayed in
	// the order in which the user entered them.
	$group_sort	= $cfg['SPAM_group_sort'];
	// Include functions
	include_once("sapanels/functions.php");
	// Start loading files and setting up arrays.
	if ($prefs_source == "db") {
		$results = mysql_query("SELECT * FROM $db_table WHERE username='$db_sauser' ORDER BY prefid");
		for($i=0; $row = mysql_fetch_array($results); $i++){
			$user_prefs_array[$i] = "$row[preference] $row[value]";
		}

	} elseif ($prefs_source == "file") {

		$user_prefs_array = file($user_prefs);

	}
	// If user_prefs_array is null, then initialize it with no data
	if(!is_array($user_prefs_array))
		$user_prefs_array = array();
	// Include all the panel files, and load
	// the user_prefs keys they want to use into an array.
	$panel_names = array();
	$prefs_keys = array();
	$dir_handle = @opendir($panels_dir);
	while($file = readdir($dir_handle)){
		if(preg_match("/^[0-9]+_(.*)$/",$file)){
			array_push($panel_names,$file);
			include_once($panels_dir."/".$file);
		}
	}
	sort($panel_names,SORT_NUMERIC);
	for($i=0;$i<sizeof($panel_names);$i++){
		preg_match("/^[0-9]+_(.*)$/",$panel_names[$i],$match);
		$panel_names[$i] = $match[1];
		eval("\$prefs_keys = array_merge(\$prefs_keys,$match[1]_keys());");
	}
	closedir($dir_handle);
	$num_panels = sizeof($panel_names);
	// Perform actions now, if there were any.

	if ($userdata['type'] == 'demo' && sizeOf($_POST) > 0) {
		echo "<center>".$T['err']['demo user']."</center>";
	} else {
	// Delete a rule
	if($_POST[delete] != ""){
		array_splice($user_prefs_array,$_POST[delete],1);
		if($prefs_source == "db") {
			mysql_query("DELETE FROM $db_table WHERE username='$db_sauser' AND preference='$_POST[preference]' AND value='$_POST[value]'");
		}
	}
	// Add a rule
	if($_POST[add] != ""){
		$address = $_POST[val];
		array_push($user_prefs_array,$_POST[key]." ".$address."\n");
		if($prefs_source == "db") {
			mysql_query("INSERT INTO $db_table(username,preference,value) VALUES('$db_sauser','$_POST[key]','$_POST[val]')");
		}
	}
	// Index $user_prefs_array
	$rules_keys = array("whitelist_from","whitelist_to","blacklist_from","whitelist_from_rcvd");
	$rules_array = array();
	$prefs_dir_array = array();
	while(list($key,$value) = each($user_prefs_array)){
		preg_match("/^([\S]+)[\s]+(.*)$/",$value,$matches);
		if(in_array($matches[1],$rules_keys)){
			$rules_array[$key] = $matches[0];
		}elseif(in_array($matches[1],$prefs_keys)){
			$prefs_dir_array[$matches[1]] = $key;
		}
	}

	// Handle Preferences Changes
	if($_POST[prefs] != ""){

		$prefs_change_array = array();
		for($i=0;$i<$num_panels;$i++){
			eval("$panel_names[$i]_apply();");
		}
		while(list($key,$value) = each($prefs_change_array)){
			$end_char = ($prefs_source == "db") ? "" : "\n";
			if(array_key_exists($key,$prefs_dir_array)){
				//$user_prefs_array[$prefs_dir_array[$key]] = $key." ".$value."\n";
				$user_prefs_array[$prefs_dir_array[$key]] = $key." ".$value.$end_char;
			}else{
				//array_push($user_prefs_array,$key." ".$value."\n");
				array_push($user_prefs_array,$key." ".$value.$end_char);
				$prefs_dir_array[$key] = (sizeof($user_prefs_array) - 1);
			}
		}
		$status_message = $T['Preferences have been updated.'];
	}

	}

	if($group_sort == "yes"){
		arsort($rules_array);
	}

	// Single call for sync if changes
	if($_POST[prefs] != "" || $_POST[add] != "" || $_POST[delete] != "")
		sync_user_prefs();

	// Display Preferences panels
	echo "<form action=\"$current_url\" method=\"POST\">";
	echo "<input type=\"hidden\" name=\"prefs\" value=\"1\" />";
	?>
	<div align=center>
	<table border="0" cellpadding="0" cellspacing="0" summary="" width=550>
	<tr>
		<td>
		<table border="0" cellspacing="0" cellpadding="0" class="tblbg" width=100%>
			<tr>
				<td>
	<?
	mesa_head("",1,1,"100%");
	echo "<tr><td class=\"tbltop\" colspan=\"2\">".$T['Preferences']."</td></tr>";
	for($i=0;$i<$num_panels;$i++){
		echo "<tr>";
			echo "<td class=\"row2\" width=\"50%\" valign=\"top\" colspan=\"2\">";
				eval($panel_names[$i]."_html();");
			echo "</td>";
		echo "</tr>";
	}
	echo "</tr>";
	echo "<tr>
		<td align=\"left\" style=\"color: red;\" class=\"content\">$status_message</td>
		<td align=\"center\" class=\"content\" width=60><input type=\"submit\" value=\"".$T['Apply']."\" /></td>
		</tr>";
	mesa_tail();
?>
				</td>
			</tr>
		</table>
		</form>
		<br><br>
		<table border="0" cellspacing="0" cellpadding="0" class="tblbg" width=100%>
			<tr>
				<td>
<?
	// Add Rule Table
	echo "<form action=\"$current_url\" method=\"POST\">";
	echo "<input type=\"hidden\" name=\"add\" value=\"1\" />";
	mesa_head("",1,1,"100%");
	echo "<tr><td colspan=\"3\" class=\"tbltop\">".$T['Add Rule']."</td></tr>
	<tr><td colspan=\"3\" class=\"row2\">
		".$T['Custom Rules Explanation']."
	</td></tr>
	<tr class=\"content\">
		<td >
			<select name=\"key\">
				<option value=\"whitelist_from\">".$T['Accept Mail From']."</option>
				<option value=\"blacklist_from\">".$T['Reject Mail From']."</option>
				<option value=\"whitelist_to\">".$T['Accept Mail Sent To']."</option>
			</select>
		</td>
		<td >
		<input type=\"text\" name=\"val\" />
		</td>
		<td align=\"center\">
			<input type=\"submit\" value=\"".$T['Add Rule']."\" />
		</td>
	</tr>";
	mesa_tail();
?>
				</td>
			</tr>
		</table>
		</form>
		<br><br>
		<table border="0" cellspacing="0" cellpadding="0" class="tblbg" width="100%">
			<tr>
				<td>
<?
	mesa_head("",1,1,"100%");
		echo "<tr><td class=\"tbltop\" colspan=\"3\">".$T['Current Rules']."</td></tr>";
		$row_toggle = 0;
		while(list($line_num,$rule) = each($rules_array)){
			preg_match("/^([\S]+)[\s]+(.*)$/",$rule,$matches);
			$key = $matches[1];
			$val = $matches[2];
			echo "<tr>";
			echo "<td class=\"row2\">";
				switch($key){
					case "whitelist_from":
						echo "<font color=\"007700\">".$T['Accept']."</font> ".$T['Mail From'];
						break;
					case "whitelist_from_rcvd":
						echo "<font color=\"007700\">".$T['Accept']."</font> ".$T['Mail From (network)'];
						break;
					case "whitelist_to":
						echo "<font color=\"007700\">".$T['Accept']."</font> ".$T['Mail Sent To'];
						break;
					case "blacklist_from":
						echo "<font color=\"FF0000\">".$T['Reject']."</font> ".$T['Mail From'];
						break;
				}
			echo "</td>";
			echo "<td class=\"row2\">$val</td>
			<td class=\"content\" align=\"center\">
				<form action=\"$current_url\" method=\"POST\">
				<input type=\"hidden\" name=\"delete\" value=\"$line_num\" />
				<input type=\"hidden\" name=\"preference\" value=\"$key\" />
				<input type=\"hidden\" name=\"value\" value=\"$val\" />
				<input type=\"submit\" value=\"".$T['Delete Rule']."\" /></form></td></tr>";

		}
	mesa_tail();
?>
				</td>
			</tr>
		</table>
		</td>
	</tr>
</table>
</div>

<?
}

function sa_add_menu(&$data) {
	global $menu;
	$menu['personal'][] = 'sapanel';
}

function sa_add_lang(&$data) {
	global $T;
	switch($data['lang']) {
		default:
			$T['menu']['sapanel'] = 'SPAM Settings';
			break;
	}
}

function sa_add_cfg(&$cfg) {
}

?>